<template>
  <a-modal
    :title="title"
    :width="600"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
  >
    <a-spin :spinning="confirmLoading">
      <a-form-model :model="model" :form="form" :rules="rules" ref="form" :label-col="{ span: 5 }" :wrapper-col="{ span: 12 }">
        <a-form-model-item label="班次名称" prop="shiftSystemName">
          <a-input placeholder="请选择组织" v-model="model.shiftSystemName"/>
        </a-form-model-item>
        <a-form-model-item label="班次" prop="company">
          <a-space v-for="(item,index) in model.shifts" :key="index">
            <j-dict-select-tag
              type="list"
              v-model="item.shift"
              style="width: 275px;"
              dictCode="shift"
              placeholder="请选择班次" />
            <a-icon style="color: red;cursor: pointer;" @click="delForm(index)" type="minus-circle" />
          </a-space>
          <a-button type="link" icon="plus" @click="addForm()">添加班次</a-button>
        </a-form-model-item>
      </a-form-model>
    </a-spin>
  </a-modal>
</template>

<script>
import { httpAction } from '@/api/manage'
import JEditor from '@/components/jeecg/JEditor'
import JSwitch from '@/components/jeecg/JSwitch'
export default {
  name: 'DeviceModal',

  components: {
    JEditor,
    JSwitch,
  },
  data() {
    return {
      title: '操作',
      visible: false,
      disable: true,
      model: {
        shifts:[],
      },
      startTime: undefined,
      endTime: undefined,
      confirmLoading: false,
      form: this.$form.createForm(this),
      rules: {
        shiftSystemName: [{ required: true, message: '请输入班制名称', trigger: 'blur' }],
      },
      url: {
        add: '/crm_shift/crmShift/add',
        edit: '/crm_shift/crmShift/edit',
      },
    }
  },
  created() {},
  methods: {
    add() {
      this.visible = true
    },
    edit(record, isEdit) {
      if (isEdit != 1) {
        this.model = Object.assign({}, record)
        console.log(this.model)
        this.startTime = record.startTime
        this.endTime = record.endTime
      }

      this.visible = true
    },
    close() {
      this.$emit('close')
      this.visible = false
      this.disable = true
      this.$refs.form.resetFields();
      this.startTime = undefined
      this.endTime = undefined
    },

    handleOk() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          let formData = Object.assign(this.model)
          console.log('formData', formData)
          //时间格式化
          httpAction(httpurl, formData, method)
            .then((res) => {
              if (res.success) {
                this.$message.success(res.message)
                this.$emit('ok')
              } else {
                this.$message.warning(res.message)
              }
            })
            .finally(() => {
              this.confirmLoading = false
              this.close()
            })
        } else {
          return false
        }
      })
    },

    handleCancel() {
      this.close()
    },
    addForm(){
      this.model.shifts.push({
        shift:''
      })
    },
    delForm(index){
      this.shifts.shifts.splice(index,1)
    }
  },
}
</script>

<style scoped></style>
